package org.sith.taskrunner.gwt.client.view;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.logical.shared.BeforeSelectionEvent;
import com.google.gwt.event.logical.shared.BeforeSelectionHandler;
import com.google.gwt.event.logical.shared.SelectionEvent;
import com.google.gwt.event.logical.shared.SelectionHandler;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.user.client.ui.*;
import org.sith.taskrunner.gwt.client.widgets.TabHeader;


public class MainWidget extends Composite {
    private DecoratedTabPanel tabPanel;

    interface MainWidgetUiBinder extends UiBinder<HTMLPanel, MainWidget> {
    }

    private static MainWidgetUiBinder ourUiBinder = GWT.create(MainWidgetUiBinder.class);
    @UiField
    VerticalPanel bottomPanel;
    @UiField
    VerticalPanel rightPanel;
    @UiField
    VerticalPanel centerPanel;
    @UiField
    VerticalPanel leftPanel;
    @UiField
    VerticalPanel topPanel;

    public MainWidget() {
        initWidget(ourUiBinder.createAndBindUi(this));
        tabPanel = new DecoratedTabPanel();
        centerPanel.add(tabPanel);
    }


    public VerticalPanel getBottomPanel() {
        return bottomPanel;
    }

    public VerticalPanel getRightPanel() {
        return rightPanel;
    }

    public void addTabInCenterPanel(String tabTitle, Widget content) {
        final TabHeader header = new TabHeader();
        tabPanel.addSelectionHandler(new SelectionHandler<Integer>() {
            @Override
            public void onSelection(SelectionEvent<Integer> integerSelectionEvent) {
                header.setEnabled(true);
            }
        });



        tabPanel.addBeforeSelectionHandler(new BeforeSelectionHandler<Integer>() {
            @Override
            public void onBeforeSelection(BeforeSelectionEvent<Integer> integerBeforeSelectionEvent) {
                header.setEnabled(false);
            }
        });
        header.setTabTitle(tabTitle);
        header.setTabCloseListener(new ClickHandler() {
            @Override
            public void onClick(ClickEvent event) {
                tabPanel.remove(tabPanel.getTabBar().getSelectedTab());

            }
        });
        tabPanel.add(content, header);

    }

    public VerticalPanel getLeftPanel() {
        return leftPanel;
    }

    public VerticalPanel getTopPanel() {
        return topPanel;
    }
}